<?php

namespace App\Jobs;

use App\Models\Posts\WxPost;
use App\Wen\Utils\PostUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UpdatePostImgCountJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $img_count = 0;
    protected $swiper_max_height = 0;
    protected $post_id = 0;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($post_id, $img_count, $swiper_max_height)
    {
        //
        $this->img_count = $img_count;
        $this->post_id = $post_id;
        $this->swiper_max_height = $swiper_max_height;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if(_empty_($this->post_id)){
            $post_ids = WxPost::where('swiper_max_height', 0)->where('img_count', '>', 0)->where('post_type', 'words')->orderBy('id', 'desc')->limit(50)->pluck('id')->toArray();
            if($post_ids){
                $data = WxPost::whereIn('id', $post_ids)->orderBy('id', 'desc')->get();
                if($data && $data->isNotEmpty()) {
                    $uid = 0;
                    $data->map(function ($v, $k) use ($uid) {
                        PostUtils::mediaProcess($v, $uid, 9);
                    });
                }
            }
        }else{
            WxPost::withoutSyncingToSearch(function (){
                $update = [];
                if($this->img_count > 0){
                    $update['img_count'] = $this->img_count;
                    if($this->swiper_max_height > 0){
                        $update['swiper_max_height'] = $this->swiper_max_height;
                    }
                }
                WxPost::where('id', $this->post_id)->update($update);
            });
        }


    }
}
